#include"StdThread.h"
#include"SystemHead.h"
#include<pthread.h>

pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond=PTHREAD_COND_INITIALIZER;
int flag=0;
void *printA(void *arg)
{
    while(1)
    {
        pthread_mutex_lock(&mutex);
        if(flag==0)
        {
            printf("A\n");
            sleep(1);
            flag=1;
            pthread_cond_signal(&cond);     
        }
        else
        {
            pthread_cond_wait(&cond,&mutex);
        }
        pthread_mutex_unlock(&mutex);
    }
}

void *printB(void *arg)
{
    while(1)
    {
        pthread_mutex_lock(&mutex);
        if(flag==1)
        {
            printf("B\n");
            sleep(1);
            flag=0;
            pthread_cond_signal(&cond);     
        }
        else
        {
            pthread_cond_wait(&cond,&mutex);
        }
        pthread_mutex_unlock(&mutex);
    }
}


int main()
{
    thread *t1=InitThread(printA,NULL);
    thread *t2=InitThread(printB,NULL);

    JoinThread(t1);
    JoinThread(t2);

    pthread_mutex_destroy(&mutex);
    pthread_cond_destroy(&mutex);

    CloseThread(t1);
    CloseThread(t2);

    return 0;
}